//package com.crm.jdy.controller.activiti;
//
//
//import com.crm.jdy.common.dto.ActivityInfoDto;
//import com.crm.jdy.common.dto.ClientChargeInfoDto;
//import com.crm.jdy.common.pojo.ActivitiBasuness;
//import com.crm.jdy.common.pojo.ActivityInfo;
//import com.crm.jdy.common.utils.api.ResultVo;
//import com.crm.jdy.service.client.ActivityInfoService;
//import com.github.pagehelper.PageInfo;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import org.activiti.api.process.model.ProcessInstance;
//import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
//import org.activiti.api.process.model.payloads.StartProcessPayload;
//import org.activiti.api.process.runtime.ProcessRuntime;
//import org.activiti.api.runtime.shared.query.Page;
//import org.activiti.api.runtime.shared.query.Pageable;
//import org.activiti.api.task.model.Task;
//import org.activiti.api.task.model.builders.TaskPayloadBuilder;
//import org.activiti.api.task.runtime.TaskRuntime;
//import org.activiti.engine.HistoryService;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.history.*;
//import org.activiti.engine.repository.Deployment;
//import org.apache.commons.io.FilenameUtils;
//import org.apache.dubbo.config.annotation.Reference;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.annotation.AuthenticationPrincipal;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.zip.ZipInputStream;
//
//@Api(tags = "activiti", description = "工作流程")
//@RestController
//@RequestMapping("/activiti")
//public class activitiController {
//
//    @Autowired
//    RepositoryService repositoryService;
//
//    @Autowired
//    ProcessRuntime processRuntime;
//
//    @Autowired
//    TaskRuntime taskRuntime;
//
//    @Autowired
//    RuntimeService runtimeService;
//
//    @Autowired
//    HistoryService historyService;
//
//
////   activiti 项目的发布和部署
//    @ApiOperation("流程定义接口")
//    @PostMapping("/uploadFileAndDeployment")
//    public ResultVo uploadFileAndDeployment(
//            @RequestParam("processFile") MultipartFile processFile,
//            @RequestParam(value = "processName",required = false) String processName){
//
//        String originalFilename = processFile.getOriginalFilename();
//        String extension = FilenameUtils.getExtension(originalFilename);
//        if (processName != null){
//            processName = originalFilename;
//        }
//        try {
//            InputStream inputStream = processFile.getInputStream();
//            Deployment deployment = null;
//            if ("zip".equals(extension)){
//                // 压缩包部署方式
//                ZipInputStream zipInputStream = new ZipInputStream(inputStream);
//                deployment = repositoryService.createDeployment().addZipInputStream(zipInputStream).name(processName).deploy();
//            }else if ("bpmn".equals(extension)){
//                // bpmn文件部署方式
//                deployment = repositoryService.createDeployment().addInputStream(originalFilename,inputStream).name(processName).deploy();
//            }
//            return ResultVo.success(deployment);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ResultVo.success();
//    }
//
////  获取流程资源文件
//    @GetMapping("/getProcessDefineXML")
//    public void getProcessDefineXML(String deploymentId, String resourceName, HttpServletResponse response){
//        try {
//            InputStream inputStream = repositoryService.getResourceAsStream(deploymentId,resourceName);
//            int count = inputStream.available();
//            byte[] bytes = new byte[count];
//            response.setContentType("text/xml");
//            OutputStream outputStream = response.getOutputStream();
//            while (inputStream.read(bytes) != -1) {
//                outputStream.write(bytes);
//            }
//            inputStream.close();
//        } catch (Exception e) {
//            e.toString();
//        }
//    }
//
////    activiti 启动实例
//    @PostMapping("/startProcess")
//    public ResultVo startProcess(
//            String processDefinitionKey,
//            String instanceName,
//            @AuthenticationPrincipal ActivitiBasuness activitiBasuness){
//        ProcessInstance processInstance = null;
//        try{
//            StartProcessPayload startProcessPayload = ProcessPayloadBuilder.start().withProcessDefinitionKey(processDefinitionKey)
//                    .withBusinessKey("businessKey")
//                    .withVariable("sponsor",activitiBasuness.getCreatePerson())
//                    .withName(instanceName).build();
//              processInstance = processRuntime.start(startProcessPayload);
//        }catch (Exception e){
//            System.out.println(e);
//            return ResultVo.failed("开启失败:"+e.getLocalizedMessage());
//        }
//        return ResultVo.success(processInstance);
//    }
//
//
////  activiti  挂起实例
//    @PostMapping("/suspendInstance/{instanceId}")
//    public ResultVo suspendInstance(@PathVariable String instanceId){
//        ProcessInstance processInstance = processRuntime.suspend(ProcessPayloadBuilder.suspend().withProcessInstanceId(instanceId).build());
//        return ResultVo.success(processInstance);
//    }
//
////    activiti  激活实例
//    @PostMapping("/resumeInstance/{instanceId}")
//    public ResultVo resumeInstance(@PathVariable String instanceId){
//        ProcessInstance processInstance = processRuntime
//                .resume(ProcessPayloadBuilder.resume().withProcessInstanceId(instanceId).build());
//        return ResultVo.success(processInstance);
//    }
//
//
////    activiti  用户完成任务
//    @PostMapping("/completeTask/{taskId}")
//    public ResultVo completeTask(@PathVariable String taskId){
//        Task task = taskRuntime.task(taskId);
//        if (task.getAssignee()==null){
//            // 说明任务需要拾取
//            taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(taskId).build());
//        }
//        taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).build());
//        return ResultVo.success();
//    }
//
//
//////    获取自身的任务
////    @GetMapping("/getTasks")
////    public ResultVo getTasks(){
////        Page<Task> taskPage = taskRuntime.tasks(Pageable.of(0, 100));
////        List<Task> tasks = taskPage.getContent();
////        List<TaskVO> taskVOS = new ArrayList<>();
////        for (Task task : tasks) {
////            TaskVO taskVO = TaskVO.of(task);
////            ProcessInstance instance = processRuntime.processInstance(task.getProcessInstanceId());
////            taskVO.setInstanceName(instance.getName());
////            taskVOS.add(taskVO);
////        }
////        return ResultVo.success(taskVOS);
////    }
////
////
//////    查询历史活动
////    @GetMapping("/getTasks")
////    public List<HistoricActivityInstanceVO> getProcessHistoryByBusinessKey(String businessKey) {
////        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
////        List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(instance.getId())
////                .orderByHistoricActivityInstanceStartTime().asc().list();
////        List<HistoricActivityInstanceVO> historicActivityInstanceVOList = new ArrayList<>();
////        historicActivityInstanceList.forEach(historicActivityInstance -> historicActivityInstanceVOList.add(VOConverter.getHistoricActivityInstanceVO(historicActivityInstance)));
////        return historicActivityInstanceVOList;
////    }
////
////
//////查询历史详情实例
////    @GetMapping("/getTasks")
////    public List<HistoricActivityInstanceVO> getProcessHistoryByBusinessKey(String businessKey) {
////        HistoricDetailQuery historicDetailQuery = historyService.createHistoricDetailQuery();
////        List<HistoricDetail> historicDetails = historicDetailQuery.processInstanceId(instanceId).orderByTime().list();
////        for (HistoricDetail hd: historicDetails) {
////            System.out.println("流程实例ID:"+hd.getProcessInstanceId());
////            System.out.println("活动实例ID:"+hd.getActivityInstanceId());
////            System.out.println("执行ID:"+hd.getTaskId());
////            System.out.println("记录时间:"+hd.getTime());
////        }
////        return historicActivityInstanceVOList;
////    }
////
//////    查询历史流程实例查询
////@GetMapping("/getTasks")
////public List<HistoricActivityInstanceVO> getProcessHistoryByBusinessKey(String businessKey) {
////
////    HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery();
////    List<HistoricProcessInstance> processInstances = historicProcessInstanceQuery.processDefinitionId(processDefinitionId).list();
////    for (HistoricProcessInstance hpi : processInstances) {
////        System.out.println("业务ID:" + hpi.getBusinessKey());
////        System.out.println("流程定义ID:" + hpi.getProcessDefinitionId());
////        System.out.println("流程定义Key:" + hpi.getProcessDefinitionKey());
////        System.out.println("流程定义名称:" + hpi.getProcessDefinitionName());
////        System.out.println("流程定义版本:" + hpi.getProcessDefinitionVersion());
////        System.out.println("流程部署ID:" + hpi.getDeploymentId());
////        System.out.println("开始时间:" + hpi.getStartTime());
////        System.out.println("结束时间:" + hpi.getEndTime());
////    }
////
////
////
////
//////    任务历史查询
////    @GetMapping("/getTasks")
////    public List<HistoricActivityInstanceVO> getProcessHisByBusinessKey(String businessKey) {
////        HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
////        List<HistoricTaskInstance> taskInstances = historicTaskInstanceQuery.taskId(taskId).list();
////        for (HistoricTaskInstance hti : taskInstances) {
////            System.out.println("开始时间:"+hti.getStartTime());
////            System.out.println("结束时间:"+hti.getEndTime());
////            System.out.println("任务拾取时间:"+hti.getClaimTime());
////            System.out.println("删除原因:"+hti.getDeleteReason());
////        }
////    }
////
//////    活动历史查询
////    @GetMapping("/getTasks")
////    public List<HistoricActivityInstanceVO> getProcessHistoryByBusinessKey(String businessKey) {
////        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
////        List<HistoricActivityInstance> historicActivityInstances = historicActivityInstanceQuery.processInstanceId(instanceId).list();
////        for (HistoricActivityInstance hai : historicActivityInstances) {
////            System.out.println("活动ID:"+hai.getActivityId());
////            System.out.println("活动类型:"+hai.getActivityType());
////            System.out.println("活动名称:"+hai.getActivityName());
////            System.out.println("任务ID:"+hai.getTaskId());
////        }
////    }
////    @GetMapping("/getTasks")
////    public List<HistoricActivityInstanceVO> getProcessHistoryByBusinessKey(String businessKey) {
////        HistoricVariableInstanceQuery historicVariableInstanceQuery = historyService.createHistoricVariableInstanceQuery();
////        List<HistoricVariableInstance> variableInstances =historicVariableInstanceQuery
////                .processInstanceId(instanceId)
////                .list();
////        for (HistoricVariableInstance hva : variableInstances) {
////            System.out.println("变量名称："+hva.getVariableName());
////            System.out.println("变量类型名称："+hva.getVariableTypeName());
////            System.out.println("变量值："+hva.getValue());
////            System.out.println("流程实例ID："+hva.getProcessInstanceId());
////            System.out.println("任务ID："+hva.getTaskId());
////        }
////    }
//
//
////    @PostMapping("/postBPMNAndDeployment")
////    public ResultVo postBPMNAndDeployment(@RequestBody AddXMLRequest addXMLRequest){
////        Deployment deploy = repositoryService.createDeployment()
////                // .addString 第一次参数的名字如果没有添加.bpmn的话，不会插入到 ACT_RE_DEPLOYMENT 表中
////                .addString(addXMLRequest.getProcessName()+".bpmn", addXMLRequest.getBpmnContent())
////                .name(addXMLRequest.getProcessName())
////                .deploy();
////        return ResultVo.success(deploy);
////    }
//
//}
